package com.yuelife.service.controller;

import com.github.pagehelper.PageInfo;
import com.yuelife.service.pojo.SCommon;
import com.yuelife.service.pojo.SStore;
import com.yuelife.service.service.SCommonService;
import com.yuelife.service.utils.AliyunOSSUtil;
import com.yuelife.service.utils.AliyunossUtils;
import com.yuelife.service.utils.FileUploadResult;
import entity.Result;
import entity.StatusCode;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/****
 * @Author:admin
 * @Description:
 * @Date 2019/6/14 0:18
 *****/
@Api(value = "SCommonController")
@RestController
@RequestMapping("/sCommon")
@CrossOrigin
public class SCommonController {

    @Autowired
    private SCommonService sCommonService;

    @Resource
    private AliyunOSSUtil aliyunOSSUtil;//第一个工具包
    @Resource
    private AliyunossUtils aliyunossUtils;//第二个工具包

    /***
     * SCommon分页条件搜索实现
     * @param sCommon
     * @param page
     * @param size
     * @return
     */
    @ApiOperation(value = "SCommon条件分页查询",notes = "分页条件查询SCommon方法详情",tags = {"SCommonController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false) @ApiParam(name = "SCommon对象",value = "传入JSON数据",required = false) SCommon sCommon, @PathVariable  int page, @PathVariable  int size){
        //调用SCommonService实现分页条件查询SCommon
        PageInfo<SCommon> pageInfo = sCommonService.findPage(sCommon, page, size);
        return new Result(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * SCommon分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @ApiOperation(value = "SCommon分页查询",notes = "分页查询SCommon方法详情",tags = {"SCommonController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用SCommonService实现分页查询SCommon
        PageInfo<SCommon> pageInfo = sCommonService.findPage(page, size);
        return new Result<PageInfo>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param sCommon
     * @return
     */
    @ApiOperation(value = "SCommon条件查询",notes = "条件查询SCommon方法详情",tags = {"SCommonController"})
    @PostMapping(value = "/search" )
    public Result<List<SCommon>> findList(@RequestBody(required = false) @ApiParam(name = "SCommon对象",value = "传入JSON数据",required = false) SCommon sCommon){
        //调用SCommonService实现条件查询SCommon
        List<SCommon> list = sCommonService.findList(sCommon);
        return new Result<List<SCommon>>(true,StatusCode.OK,"查询成功",list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @ApiOperation(value = "SCommon根据ID删除",notes = "根据ID删除SCommon方法详情",tags = {"SCommonController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        //调用SCommonService实现根据主键删除
        sCommonService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 修改SCommon数据
     * @param sCommon
     * @param id
     * @return
     */
    @ApiOperation(value = "SCommon根据ID修改",notes = "根据ID修改SCommon方法详情",tags = {"SCommonController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @PutMapping(value="/{id}")
    public Result update(@RequestBody @ApiParam(name = "SCommon对象",value = "传入JSON数据",required = false) SCommon sCommon, @PathVariable Integer id){
        //设置主键值
        sCommon.setCommonId(id);
        //调用SCommonService实现修改SCommon
        sCommonService.update(sCommon);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /***
     * 新增SCommon数据
     * @param sCommon
     * @return
     */
    @ApiOperation(value = "SCommon添加",notes = "添加SCommon方法详情",tags = {"SCommonController"})
    @PostMapping(value = "/add")
    public Result add( @ApiParam(name = "SCommon对象",value = "传入JSON数据",required = true) SCommon sCommon){
        //调用SCommonService实现添加SCommon
        sCommon.setCreateTime(new Date());
        sCommonService.add(sCommon);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    @PostMapping("/addcommon")
    public Result addcommon(@RequestParam(value = "file",required = false) MultipartFile file, SCommon sCommon)throws IOException {
        FileUploadResult upload = aliyunossUtils.upload(file);
        String img = upload.getName();
        sCommon.setImg(img);
        sCommon.setCreateTime(new Date());
        sCommonService.add(sCommon);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /***
     * 根据ID查询SCommon数据
     * @param id
     * @return
     */
    @ApiOperation(value = "SCommon根据ID查询",notes = "根据ID查询SCommon方法详情",tags = {"SCommonController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @GetMapping("/{id}")
    public Result<SCommon> findById(@PathVariable Integer id){
        //调用SCommonService实现根据主键查询SCommon
        SCommon sCommon = sCommonService.findById(id);
        return new Result<SCommon>(true,StatusCode.OK,"查询成功",sCommon);
    }

    /***
     * 查询SCommon全部数据
     * @return
     */
    @ApiOperation(value = "查询所有SCommon",notes = "查询所SCommon有方法详情",tags = {"SCommonController"})
    @GetMapping
    public Result<List<SCommon>> findAll(){
        //调用SCommonService实现查询所有SCommon
        List<SCommon> list = sCommonService.findAll();
        return new Result<List<SCommon>>(true, StatusCode.OK,"查询成功",list) ;
    }


    /**
     * 按店铺id查询评论
     * @param storeId
     * @return
     */
    @PostMapping("/StoreCommon")
    @ResponseBody
    public Result<List<SCommon>> selectSCommonByStoreId(@RequestParam(value = "storeId",required = true) Integer storeId){
        List<SCommon> sCommons = sCommonService.selectSCommonByStoreId(storeId);
        return new Result<List<SCommon>>(true,StatusCode.OK,"店铺评论查询成功",sCommons);
    }

    /**
     * 按商品查询评论
     * @param serviceId
     * @return
     */
    @GetMapping("/commentList/{serviceId}")
    public Result<List<SCommon>> selectSCommonByserviceId(@PathVariable("serviceId") Integer serviceId){
        List<SCommon> sCommons = sCommonService.selectSCommonByserviceId(serviceId);
        return new Result<List<SCommon>>(true,StatusCode.OK,"商品评论查询成功",sCommons);
    }




}
